﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Framework.Data;
using Framework.Common;
using Dal.Entities;
namespace Web.Controllers
{
    [ValidateInput(false)]
    public class AccountController : Controller
    {

        public ActionResult LogOff()
        {
            //Session["user"] = null;
            //Session["doctype"] = null;
            FormsAuthentication.SignOut();
            return RedirectToAction("index", "login");
        }

        
        [MyAuthAttribute(Role = 1)]
        [HttpGet]
        public ActionResult CreateUser()
        {
            int userid = TypeConvertHelper.StrToInt(Request["userid"]);
            Dal.Entities.User user = new Dal.Entities.User();
            if (userid > 0)
            {
                user = RepositoryFactory.GetRepository().GetByKey<Dal.Entities.User>(userid);
            }
            var regionProvinces = RepositoryFactory.GetRepository().Get<Dal.Entities.Region>(i => i.depth == 2).ToList();
            regionProvinces.Insert(0, new Dal.Entities.Region { Id = -1, BriefName = "请选择" });
            ViewBag.Region2 = regionProvinces;

            return View(user);
        }

        [HttpPost]
        [MyAuthAttribute(Role = 1)]
        public ActionResult CreateUser(FormCollection form)
        {
            var IRepository = RepositoryFactory.GetRepository();
            Dal.Entities.User user = new Dal.Entities.User();

            if (TryUpdateModel<Dal.Entities.User>(user, form))
            {
                if (user.ID == 0)
                {
                    if (IRepository.Count<User>(i => i.UserName == user.UserName) == 0)
                    {
                        if (IRepository.AddThenSaveChange(user) > 0)
                        {
                            Common.AlertRedirect("添加成功", "/account/manage");
                        }
                    }
                    else
                    {
                        Common.AlertRedirect("添加失败", "/account/manage");
                    }
                }
                else
                {
                    Dal.Entities.User newuser = new Dal.Entities.User { ID = user.ID };
                    IRepository.Attach(newuser);
                    UpdateModel<Dal.Entities.User>(newuser, form);
                    if (IRepository.Save() > 0)
                    {
                        Common.AlertRedirect("更改成功", "/account/manage");
                    }
                    else
                    {
                        Common.AlertRedirect("更改失败", "/account/manage");
                    }

                }
            }

            return null;
        }

        [MyAuthAttribute(Role = 1)]
        public ActionResult DeleteUser(int userID)
        {
            var IRepository = RepositoryFactory.GetRepository();
            Dal.Entities.User user = new Dal.Entities.User { ID = userID };
            IRepository.DeleteThenSaveChange(user);
            return RedirectToAction("manage");
        }
        
        [MyAuthAttribute(Role = 1)]
        public ActionResult Manage()
        {
            int page = TypeConvertHelper.StrToInt(Request["pageIndex"], 1);
            int recordcount;
            var result = RepositoryFactory.GetRepository().GetPaged<Dal.Entities.User, int>(page - 1, 10, out recordcount, i => i.ID);
            ViewBag.Users = result;
            ViewBag.RecordCount = recordcount;
            ViewBag.PageIndex = page;
            return View();
        }


    }
}
